<template>
	<view>
		<!-- <web-view :src="myurl" @load="loadWeb"></web-view> -->
		<button class='auth-btn-login' type='primary' open-type="getUserInfo"
		 :withCredentials="true" lang="zh_CN" @getuserinfo="wxGetUserInfo">授权
		</button>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				// myurl: 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx8994b4a7a8e928de&redirect_uri=https://api.jsl123.com&response_type=code&scope=snsapi_base&state=STATE'
				// myurl: 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx8994b4a7a8e928de&redirect_uri=https://binnie-37s7d.service.tcloudbase.com&response_type=code&scope=snsapi_base&state=STATE'
				myurl: 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx8994b4a7a8e928de&redirect_uri=https://www.baidu.com&response_type=code&scope=snsapi_base&state=STATE'
				// myurl: 'https://www.baidu.com'
			}
		},
		onLoad() {
		},
		onShow() {
			if (!uni.getStorageSync("isFirstUse")) {
				uni.switchTab({
					url: "../index/index"
				})
			}
		},
		methods: {
			/**
			 * 授权页加载完毕
			 */
			loadWeb(e) {
				console.log(e, '加载完毕了')
				let url = e.detail.src
				let start = url.indexOf("=")
				let end = url.indexOf("&")
				let codes = url.substring(start + 1, end)
				this.$utils.setData("codeAttention", codes)
				uni.switchTab({
					url: "../index/index"
				})
			},
			/**
			 * 授权
			 */
			wxGetUserInfo(e) {
				this.$utils.setData("encryptedData", e.detail.encryptedData)
				this.$utils.setData("iv", e.detail.iv)
				this.$utils.setData("isFirstUse", false) //记录是否第一次授权  false:表示不是第一次授权
				uni.switchTab({
					url: "../index/index"
				})
			},
		}
	}
</script>

<style>
	.auth-btn-login {
		position: fixed;
		top: 200rpx;
		width: 700rpx;
		left: 25rpx;
	}
</style>
